package com.cms.biz.queryAnalyse.query;

import java.sql.ResultSet;
import java.sql.Statement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.stereotype.Repository;

import com.common.util.EMPSQLUtil;
import com.efounder.builder.base.data.DataSetUtils;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.eai.data.JParamObject;
import com.efounder.eai.data.JResponseObject;
import com.efounder.sql.JConnection;
import com.efounder.sql.JStatement;
import com.enums.EMPQueryParamEnum;
import com.mrp.persistence.queryAnalyse.pageQuery.bean.EMPPageQuery;
import com.pub.util.EMPLimitConvertUtil;


/**
 * <p>Title: 项目查询分析</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2010</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
@Repository
public class EMPUnpaidContractInfoQuery {

	@Autowired
	private JdbcTemplate                  jdbcTemplate;
	
	protected     String                   msTableName = "";
	
	protected     String                  msTableName1 = "";
	
	public JResponseObject QueryObject(JParamObject PO){
		JConnection         conn = null;
		JStatement          stmt = null;
		EFDataSet   queryDataset = null;
		EFRowSet     queryRowset = EFRowSet.getInstance();
		JResponseObject       RO = null;
		try {
			conn = JConnection.getInstance(jdbcTemplate.getDataSource().getConnection());
			conn.setAutoCommit(true);
			stmt = conn.createStatement();
			
			// 获取查询数据
			queryDataset = convertDataSet(stmt, PO);
			queryRowset.setExtProperty(EMPQueryParamEnum.PAGEQUERY, PO.getValue(EMPQueryParamEnum.PAGEQUERY, null));
			queryRowset.setDataSet(EMPQueryParamEnum.QUERYRESULT, queryDataset);
			// 封装返回数据
		   	RO = new JResponseObject(queryRowset, 0);
		} catch(Exception ce) {
			ce.printStackTrace();
			RO = new JResponseObject(null, -1, ce);
		} finally {
			EMPSQLUtil.closeAllResources(null, stmt, conn);
		}
		return RO;
	}
	
	/**
	 * 组织数据的过程
	 * @throws Exception
	 * @return TreeTableDataManager
	 */
	public EFDataSet convertDataSet(Statement stmt, JParamObject po) throws Exception {
		EFDataSet queryDataset = null;
		EFDataSet    objcolsDS = EFDataSet.getInstance("SYS_OBJCOLS");
		EFDataSet   newDataset = EFDataSet.getInstance();
		String          F_HTMC = po.GetValueByParamName("F_HTMC", "");
		String          F_DJLX = po.GetValueByParamName("F_DJLX", "");
		EFRowSet        rowset = null;
		ResultSet           rs = null;
		String          strSql = ""; 
		EMPPageQuery pageQuery = (EMPPageQuery) po.getValue(EMPQueryParamEnum.PAGEQUERY, "");

		strSql = " select COL_ID, COL_MC " + " from SYS_OBJCOLS" + " where OBJ_ID = 'HTXXZD' and F_STAU = '1'";

		rs = stmt.executeQuery(strSql);
		objcolsDS = DataSetUtils.resultSet2DataSet(rs, objcolsDS);
		
		strSql = "select ";
		for(int i = 0; i < objcolsDS.getRowCount(); i++) {
			rowset = objcolsDS.getRowSet(i);
			strSql += "HTXXZD." + rowset.getString("COL_ID", "") + " as " + rowset.getString("COL_ID", "") + ",";
		}
		strSql += " HTFKXX_V.F_HTJE as F_YFK, (HTXXZD.F_HTJE - HTFKXX_V.F_HTJE) as F_WFK"
			    + " from HTXXZD "
			    + " left join HTFKXX_V on HTFKXX_V.F_HTBH = HTXXZD.F_HTBH"
			    + " where F_HTMC like '" + F_HTMC + "%' and (HTXXZD.F_HTJE - IFNULL(HTFKXX_V.F_HTJE,0) > 0) and "
			    + " F_DJLX like '" + F_DJLX + "%'"
			    + " order by F_HTBH desc";

		rs = stmt.executeQuery(strSql);
		queryDataset = DataSetUtils.resultSet2DataSet(rs, queryDataset);
		
		if(pageQuery == null) return queryDataset;
		
		pageQuery.setTotalCount(queryDataset.getRowCount());
		newDataset = EMPLimitConvertUtil.getDataSetLimit(queryDataset, pageQuery.getPageNum(), pageQuery.getNumPerPage());
		return newDataset;
	}
}
